//
//  RHEfficiencyRankCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/12.
//

#import "RHEfficiencyRankCell.h"
#import "RHEfficiencyRankItemCell.h"
@interface RHEfficiencyRankCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectView;


@end
@implementation RHEfficiencyRankCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

- (void)setEfficiencyRankArr:(NSArray *)efficiencyRankArr
{
    _efficiencyRankArr = efficiencyRankArr;
    
    [self.collectView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(60*efficiencyRankArr.count+20);
    }];
    
    NSMutableArray *arr = [NSMutableArray array];
    __block NSInteger total = 0;
    
    [efficiencyRankArr enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
        total += [dic[@"value"]integerValue];
    }];
    [self.collectView reloadData];
}

-(void)setupUI
{
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView rounded:8 width:1 color:kBorderColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.text = @"供载率".rh_localized;
    titleLab.textColor = kCOLOR_HEX(@"#171A1D");
    titleLab.textAlignment = NSTextAlignmentRight;
    titleLab.font = kBOLDSYSTEMFONT(16);
    [bgView addSubview:titleLab];
 
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.left.mas_offset(15);
    }];
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.16);
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(titleLab.mas_bottom).offset(15);
        make.height.offset(1);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectView.backgroundColor = [UIColor whiteColor];
    _collectView.layer.cornerRadius = 8;
    _collectView.layer.masksToBounds = YES;
    _collectView.showsVerticalScrollIndicator = NO;
    _collectView.showsHorizontalScrollIndicator = NO;
    _collectView.delegate = self;
    _collectView.dataSource = self;
    [_collectView registerClass:[RHEfficiencyRankItemCell class] forCellWithReuseIdentifier:@"RHEfficiencyRankItemCell"];
    [bgView addSubview:_collectView];
    
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.offset(60*4+10);
        make.bottom.mas_offset(0);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.efficiencyRankArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHEfficiencyRankItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHEfficiencyRankItemCell" forIndexPath:indexPath];
    NSArray *colors = @[@"55E4B9",@"589CFB",@"FDAF37"];
    NSArray *icons = @[@"device_loadRate_battery",@"device_loadRate_grid",@"device_loadRate_solar"];
    cell.icon.image = [UIImage imageNamed:icons[indexPath.item%colors.count]];
    cell.color = colors[indexPath.item % colors.count];
    cell.dic = self.efficiencyRankArr[indexPath.item];
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kUI_WINDOW_W -15*2 , 60);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 10, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

@end
